//
//  ViewController.m
//  MainThreadSentinel
//
//  Created by vedon on 6/27/16.
//  Copyright © 2016 vedon. All rights reserved.
//
#define TICK   NSDate *startTime = [NSDate date]
#define TOCK   NSLog(@"Time: %f", -[startTime timeIntervalSinceNow])

#import "ViewController.h"
#import "TestObject.h"
#import "VDMainThreadSentinel.h"
#import "STLDequeueArray.h"


@interface ViewController ()
@property (strong,nonatomic) STLDequeueArray *stdArray;
@property (strong,nonatomic) NSMutableArray *mutableArray;
@end

static float consumeTime1 = 0;
static float consumeTime2 = 0;
static NSInteger sentinel = 0;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    /**
     *  <#Description#>
     */
//    [self testTheSTLDequeueAndTheNSMutableArrayPerformance];
    
    
    /**
     *  <#Description#>
     */
    [self testRunLoopAction];
    
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)testRunLoopAction
{
    for (int i = 0; i < 10000; i++)
    {
        
//        TestObject *object = [[TestObject alloc] init];
//        [[VDMainThreadSentinel shareDispatcher] enqueue:object];
        
        if (i % 2 == 0)
        {
//            [self performSelectorOnMainThread:@selector(log:) withObject:@(i) waitUntilDone:NO];
//            [self performSelectorOnMainThread:@selector(test) withObject:@(i) waitUntilDone:NO];
//            [self performSelector:@selector(log:) withObject:@(i) afterDelay:2.1];
        }
        else
        {
//            NSLog(@"i = %d", i);
            [[VDMainThreadSentinel shareDispatcher] enqueueBlock:^{
                
                [self log:@(i)];
            }];
        }

    }
    
    NSLog(@"Start");
}

- (void)log:(NSNumber *)count
{
    if (sentinel > count.integerValue)
    {
//        assert(0);
        
    }
    NSLog(@"%@",count);
    sentinel = count.integerValue;
}

- (void)test
{
    NSLog(@"fuick");
}

- (void)testTheSTLDequeueAndTheNSMutableArrayPerformance
{
    self.stdArray = [[STLDequeueArray alloc] init];
    self.mutableArray = [NSMutableArray array];
    
    
    for (int i = 0 ; i < 100; i++)
    {
        [self testMutableArray];
    }
    
    NSLog(@"time cost %f",consumeTime1/20);
    
    for (int i = 0 ; i < 100; i++)
    {
        [self testSTLArray];
    }
    
    NSLog(@"time cost %f",consumeTime2/20);

}

- (void)testMutableArray
{
    NSDate *startTime = [NSDate date];
    for (int i = 0; i < 10000; i++)
    {
        [self.mutableArray addObject:[NSObject new]];
    }
    
    for (int i = 0; i < 10000; i++)
    {
        [self.mutableArray removeLastObject];
    }

    
    CGFloat time = -[startTime timeIntervalSinceNow];
    consumeTime1 += time;

}


- (void)testSTLArray
{
    NSDate *startTime = [NSDate date];
    for (int i = 0; i < 10000; i++)
    {
        [self.stdArray addObject:[NSObject new]];
    }
    
    for (int i = 0; i < 10000; i++)
    {
        [self.stdArray removeLastObject];
    }

    
    CGFloat time = -[startTime timeIntervalSinceNow];
    consumeTime2 += time;
}


@end
